export const getWeek = function (n, type) {
  // 周
  let now = new Date();
  let day = now.getDay(); //返回星期几的某一天;
  if (!type) {
    if (n == 1) {
      let dayNumber = day == 0 ? 0 : 7 - day;
      now.setDate(now.getDate() + dayNumber + 1);
    } else if (n == -1) {
      let dayNumber = day == 0 ? 6 : day - 1;
      now.setDate(now.getDate() - dayNumber - 7);
    } else {
      let dayNumber = day == 0 ? 6 : day - 1;
      now.setDate(now.getDate() - dayNumber);
    }
  } else {
    if (n == 1) {
      let dayNumber = day == 0 ? 0 : 7 - day;
      now.setDate(now.getDate() + dayNumber + 1 + 6); // 在周开始的日期上+6天=周结束
    } else if (n == -1) {
      let dayNumber = day == 0 ? 6 : day - 1;
      now.setDate(now.getDate() - dayNumber - 7 + 6);
    } else {
      let dayNumber = day == 0 ? 0 : 7 - day;
      now.setDate(now.getDate() + dayNumber);
    }
  }
  let date = now.getDate();
  let month = now.getMonth() + 1;

  let s =
    now.getFullYear() +
    "-" +
    (month < 10 ? "0" + month : month) +
    "-" +
    (date < 10 ? "0" + date : date);
  return s;
  //这里调用方法
  // 	this.getWeek(-1, 0), // 获取上周开始日期
  // 	this.getWeek(-1, 1), // 获取上周结束日期
  // 	this.getWeek(0, 0), // 获取本周开始日期
  // 	this.getWeek(0, 1), // 获取本周结束日期
  // 	this.getWeek(1, 0), // 获取下周开始日期
  // 	this.getWeek(1, 1), // 获取下周结束日期
};

/**获取本月、上月、下月的起始日、结束日 (不传或0代表本月，-1代表上月，1代表下月)*/
// 1.调用方法 getMonth("s",0)、getMonth("e",0)
// 2.type 为"s"代表开始日期,type为"e"代表结束日期
// 3.dates  不传或0代表本月，-1代表上月，1代表下月
export const getMonth = (type, months) => {
  var d = new Date();
  var year = d.getFullYear();
  var month = d.getMonth() + 1;
  if (Math.abs(months) > 12) {
    months = months % 12;
  }
  if (months != 0) {
    if (month + months > 12) {
      year++;
      month = (month + months) % 12;
    } else if (month + months < 1) {
      year--;
      month = 12 + month + months;
    } else {
      month = month + months;
    }
  }
  month = month < 10 ? "0" + month : month;
  var date = d.getDate();
  var firstday = year + "-" + month + "-" + "01";
  var lastday = "";
  if (
    month == "01" ||
    month == "03" ||
    month == "05" ||
    month == "07" ||
    month == "08" ||
    month == "10" ||
    month == "12"
  ) {
    lastday = year + "-" + month + "-" + 31;
  } else if (month == "02") {
    if (
      (year % 4 == 0 && year % 100 != 0) ||
      (year % 100 == 0 && year % 400 == 0)
    ) {
      lastday = year + "-" + month + "-" + 29;
    } else {
      lastday = year + "-" + month + "-" + 28;
    }
  } else {
    lastday = year + "-" + month + "-" + 30;
  }
  var day = "";
  if (type == "s") {
    day = firstday;
  } else {
    day = lastday;
  }
  return day;
};

export const formatDate = (value) => {
  if (value) {
    let date = new Date(value * 1000);
    //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? "0" + MM : MM; //月补0
    let d = date.getDate();
    d = d < 10 ? "0" + d : d; //天补0
    let h = date.getHours();
    h = h < 10 ? "0" + h : h; //小时补0
    let m = date.getMinutes();
    m = m < 10 ? "0" + m : m; //分钟补0
    let s = date.getSeconds();
    s = s < 10 ? "0" + s : s; //秒补0
    return y + "-" + MM + "-" + d; //年月日
  }
};

export const formatDatetime = (value) => {
  let date = new Date(value * 1000);
  //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let y = date.getFullYear();
  let MM = date.getMonth() + 1;
  MM = MM < 10 ? "0" + MM : MM; //月补0
  let d = date.getDate();
  d = d < 10 ? "0" + d : d; //天补0
  let h = date.getHours();
  h = h < 10 ? "0" + h : h; //小时补0
  let m = date.getMinutes();
  m = m < 10 ? "0" + m : m; //分钟补0
  let s = date.getSeconds();
  s = s < 10 ? "0" + s : s; //秒补0
  return y + "-" + MM + "-" + d + "\xa0" + h + ":" + m + ":" + s; //年月日
};
// 转换时间戳年月日 时分
export const formatDatetimePicker = (value) => {
  let date = new Date(value * 1000);
  //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let y = date.getFullYear();
  let MM = date.getMonth() + 1;
  MM = MM < 10 ? "0" + MM : MM; //月补0
  let d = date.getDate();
  d = d < 10 ? "0" + d : d; //天补0
  let h = date.getHours();
  h = h < 10 ? "0" + h : h; //小时补0
  let m = date.getMinutes();
  m = m < 10 ? "0" + m : m; //分钟补0
  let s = date.getSeconds();
  s = s < 10 ? "0" + s : s; //秒补0
  return y + "-" + MM + "-" + d + "\xa0" + h + ":" + m ; //年月日
};
export const timeYMD = (value) => {
  let date = new Date(value * 1000);
  //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let y = date.getFullYear();
  let MM = date.getMonth() + 1;
  MM = MM < 10 ? "0" + MM : MM; //月补0
  let d = date.getDate();
  d = d < 10 ? "0" + d : d; //天补0
  let h = date.getHours();
  h = h < 10 ? "0" + h : h; //小时补0
  let m = date.getMinutes();
  m = m < 10 ? "0" + m : m; //分钟补0
  let s = date.getSeconds();
  s = s < 10 ? "0" + s : s; //秒补0
  return h + "-" + m;
};
// 根据生日计算周岁
export const calculateAge = (birthdate) => {
// 将出生日期字符串转换为日期对象
const birthDateObj = new Date(birthdate);
// 获取当前日期
const currentDate = new Date();
// 计算年龄
let age = currentDate.getFullYear() - birthDateObj.getFullYear();
// 考虑生日还未到的情况
const currentMonth = currentDate.getMonth() + 1; // 月份是从0开始的，所以要加1
const birthMonth = birthDateObj.getMonth() + 1;
if (currentMonth < birthMonth || (currentMonth === birthMonth && currentDate.getDate() < birthDateObj.getDate())) {
  age--;
}
return age;
};
export const formatImg = (value, tag = true) => {
  if (value && value.indexOf("<img") !== -1) {
    if (tag) {
      return value.replace(
        /\<img/gi,
        "<img class='richTextImg' style='max-width:100%;height:auto'"
      );
    } else {
      return value.replace(/\<img/gi, "<img class='richTextImg'");
    }
  }
  return value;
};
export const formatFileSize = (size) => {
  const units = ["B", "KB", "MB", "GB", "TB"];
  let unitIndex = 0;
  while (size >= 1024 && unitIndex < units.length - 1) {
    size /= 1024;
    unitIndex++;
  }

  const sizes = Number(size).toFixed(2) + units[unitIndex];
  return sizes;
};
